package com.geostar.gmp.model.factory;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件名:AbstractMybatis.java
 * 功能: DAO层的超类，主要用于sqlsession对象的创建与关闭
 * 目前支持方法
 * --------------------------------------
 * 初始版本 
 * 作者: yesong
 * 时间: Apr 4, 2014
 */
public class SessionFactory {

	private static Logger logger = LoggerFactory.getLogger(SessionFactory.class);
	private static final String MYBATIS_XML = "mybatis.cfg.xml";
	
	public static SqlSession createSession(){
		SqlSession sqlSession = null;
		try {
			Reader reader = Resources.getResourceAsReader(MYBATIS_XML);
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = builder.build(reader);
			sqlSession = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(">>>>>>>>>MyBaits读取文件失败<<<<<<<<<");
		}
		return sqlSession;
	}
	
	public static void closeSession(SqlSession sqlSession){
		if(sqlSession != null){
			sqlSession.close();
		}
	}
}
